package com.itany.nmms.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itany.nmms.constant.DictConstant;
import com.itany.nmms.constant.StatusConstant;
import com.itany.nmms.dao.StaffMapper;
import com.itany.nmms.entity.Staff;
import com.itany.nmms.entity.StaffExample;
import com.itany.nmms.exception.CodeErrorException;
import com.itany.nmms.exception.RequestParameterErrorException;
import com.itany.nmms.exception.StaffNotExistException;
import com.itany.nmms.service.StaffService;
import com.itany.nmms.util.MD5Util;
import com.itany.nmms.util.ParameterUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author 石小俊
 * @date 2024年04月17日 18:11
 */
@Service
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
public class StaffServiceImpl implements StaffService {

    @Autowired
    private StaffMapper staffMapper;

    @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
    @Override
    public Staff login(String loginName, String password, String role, String code, String image) throws RequestParameterErrorException, CodeErrorException, StaffNotExistException {
        // 非空校验
        if (ParameterUtil.isNull(loginName) || ParameterUtil.isNull(password) || ParameterUtil.isNull(role) || ParameterUtil.isNull(code) || ParameterUtil.isNull(image)) {
            throw new RequestParameterErrorException("请求参数有误");
        }

        // 校验验证码
        if (!code.equals(image)) {
            throw new CodeErrorException("验证码不正确");
        }

        // 去数据库进行查询,判断当前信息是否正确
        StaffExample example = new StaffExample();
        example.or()
                .andLoginNameEqualTo(loginName)
                .andPasswordEqualTo(MD5Util.md5(password))
                .andRoleEqualTo(role)
                .andIsValidEqualTo(StatusConstant.STAFF_IS_VALID_YES);  // 员工必须在职才能登录
        List<Staff> staffs = staffMapper.selectByExample(example);
        if (staffs.isEmpty()) {
            throw new StaffNotExistException("账号或密码错误");
        }
        return staffs.get(0);
    }

    @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
    @Override
    public PageInfo<Staff> findPage(String pageNo, String pageSize, Staff staffParam) {
        // String staffName, String loginName, String phone, String email, String deptId, String role, String isValid
        if (ParameterUtil.isNull(pageNo)) {
            pageNo = DictConstant.STAFF_DEFAULT_PAGE_NO;
        }
        if (ParameterUtil.isNull(pageSize)) {
            pageSize = DictConstant.STAFF_DEFAULT_PAGE_SIZE;
        }
        PageHelper.startPage(Integer.parseInt(pageNo), Integer.parseInt(pageSize));

        Staff param = new Staff();
        param.setStaffName(ParameterUtil.escapeString(staffParam.getStaffName()));
        param.setLoginName(ParameterUtil.escapeString(staffParam.getLoginName()));
        param.setPhone(ParameterUtil.escapeString(staffParam.getPhone()));
        param.setEmail(ParameterUtil.escapeString(staffParam.getEmail()));
        param.setDeptId(staffParam.getDeptId());
        param.setRole(staffParam.getRole());
        param.setIsValid(staffParam.getIsValid());
        List<Staff> staffs = staffMapper.selectByParams(param);
        PageInfo<Staff> staffPage = new PageInfo<>(staffs);
        return staffPage;
    }
}
